
import pyautogui
import pyperclip
import time
import sys


def open_app(app_path):
    pyautogui.hotkey('win', 'r')
    time.sleep(1)
    # 全选并清空搜索框内容
    pyautogui.hotkey('ctrl', 'a')
    time.sleep(1)
    pyautogui.press('backspace')
    time.sleep(1)
    pyautogui.typewrite(app_path)
    time.sleep(1)
    pyautogui.press('enter')
    pyautogui.press('enter')
    time.sleep(5)
    print('打开豆包电脑版')


def open_baidu_baike():
    search_box_x = 192
    search_box_y = 66
    pyautogui.click(search_box_x, search_box_y)
    # # 全选并清空搜索框内容
    # pyautogui.hotkey('ctrl', 'a')
    # time.sleep(1)
    # pyautogui.press('backspace')
    # time.sleep(1)
    pyautogui.typewrite('https://baike.baidu.com')
    pyautogui.press('enter')
    pyautogui.press('enter')
    time.sleep(5)
    print('打开百度百科')


def search_on_baike(query):
    # 假设搜索框在屏幕上的位置，需要根据实际情况调整
    search_box_x = 600
    search_box_y = 250
    pyautogui.click(search_box_x, search_box_y)
    # # 全选并清空搜索框内容
    # pyautogui.hotkey('ctrl', 'a')
    # time.sleep(1)
    # pyautogui.press('backspace')
    # time.sleep(1)
    # 将文本复制到剪贴板
    pyperclip.copy(query)
    pyautogui.hotkey('ctrl', 'v')
    time.sleep(2)
    pyautogui.press('enter')
    pyautogui.press('enter')
    time.sleep(5)
    print('搜索完成')


def copy_text_content():
    pyautogui.click(187, 292)
    pyautogui.hotkey('ctrl', 'a')
    time.sleep(3)
    pyautogui.hotkey('ctrl', 'c')
    time.sleep(3)
    content = pyperclip.paste()
    return content


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("请提供要搜索的字符串作为参数。")
        sys.exit(1)
    search_query = sys.argv[1]
    # 请替换为豆包电脑版的实际路径
    doubao_app_path = "D:\Doubao\Doubao.exe"
    open_app(doubao_app_path)
    open_baidu_baike()
    search_on_baike(search_query)
    text_content = copy_text_content()
    print(text_content)